package demo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.openhtmltopdf.extend.FSSupplier;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder.FontStyle;

import utils.My;

/**
 * 
 * 代码摘抄自: <https://github.com/danfickle/openhtmltopdf/blob/open-dev-v1/docs/integration-guide.md>
 * [含有中文字符的html输出pdf有乱码 #129](https://github.com/danfickle/openhtmltopdf/issues/129)
 * 
 * 
 * @author Wxg
 *
 */
public class SimpleUsage {
	public static void main(String[] args) {
//		new SimpleUsage().exportToPdfBox("file:///Users/user/path-to/document.xhtml"
//				, "/Users/user/path-to/output.pdf");
		// http://192.168.3.5:8000/_my/106weasyprint.html
		// String url = "http://192.168.3.5:8000/_my/106weasyprint.html";
		String url = My.prop("mainconfig.properties").getProperty("url");
		String output = "D:\\wxg\\openhtmltopdf\\output1.pdf";
		new SimpleUsage().exportToPdfBox(url, output);
	}

	public void exportToPdfBox(String url, String out) {
		OutputStream os = null;

		try {
			os = new FileOutputStream(out);

			try {
				// There are more options on the builder than shown below.
				PdfRendererBuilder builder = new PdfRendererBuilder();
				
				appendFonts(builder);

				builder.withUri(url);
				builder.toStream(os);
				builder.run();

			} catch (Exception e) {
				e.printStackTrace();
				// LOG exception
			} finally {
				try {
					os.close();
				} catch (IOException e) {
					// swallow
				}
			}
		} catch (IOException e1) {
			e1.printStackTrace();
			// LOG exception.
		}
	}
	
	/**
	 * 添加字体
	 * 以下是之前的总结：
	 * 1) 可以指定中文名称的"fontFamily"
	 * 2) 目前只能识别"*.ttf"格式的字体, ".ttc"格式的不是别: Couldn't load font. Please check that it is a valid truetype font.
	 * 
	 * @param builder
	 */
	private void appendFonts(PdfRendererBuilder builder){
		String fontConf = "fonts/fonts.path";
		String[] fontspaths = My.lines(fontConf);
		if(null != fontspaths){
			String[] confs = null;
			for(String line : fontspaths){
				confs = line.split(",");
				builder.useFont(new ClientFSSupplier(confs[0].trim()), 
						confs[1].trim(),
						Integer.parseInt(confs[2].trim()),
						fs(confs[3].trim()),
						true);
			}
		}
	}
	
	/**
	 * 简单转换
	 * @param fontStyle
	 * @return
	 */
	private FontStyle fs(String fontStyle){
		if("normal".equals(fontStyle)){
			return FontStyle.NORMAL;
		}else if("italic".equals(fontStyle)){
			return FontStyle.NORMAL;
		}else if("oblique".equals(fontStyle)){
			return FontStyle.NORMAL;
		}else{
			return FontStyle.NORMAL;
		}
	}
	
	public class ClientFSSupplier implements FSSupplier<InputStream>{
		private String fontPosition;
		public ClientFSSupplier(String fontPosition){
			this.fontPosition = fontPosition;
		}
		@Override
		public InputStream supply() {
			InputStream is = null;
			try {
				// 警告: Couldn't load font. Please check that it is a valid truetype font.
				is = new FileInputStream(fontPosition);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			return is;
		}
	}
}
